//
//  FBCustomNavigationViewController.m
//  FindBook
//
//  Created by zhaoxu_liu on 13-11-1.
//  Copyright (c) 2013年 lzx. All rights reserved.
//

#import "FBCustomNavigationViewController.h"

#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

@interface FBCustomNavigationViewController ()

@end

@implementation FBCustomNavigationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *bgImage = [UIImage imageNamed:@"navbar_bg.png"];
    
    if (IOS_VERSION >= 7.0) {
        bgImage = [UIImage imageNamed:@"navbar_bg_ios7.png"];
    }
    
    [self.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIBarButtonItem*) createBackButton

{
    UIImage *buttonImage = [UIImage imageNamed:@"navigation_back.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.exclusiveTouch = YES;
    [button setImage:buttonImage forState:UIControlStateNormal];
    
    if (IOS_VERSION < 7)
    {
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    }
    
    button.frame = CGRectMake(0, 0, 26, 21);
    [button addTarget:self action: @selector(handleBackButton)
     forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return customBarItem;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
    if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1)
    {
        
        viewController.navigationItem.leftBarButtonItem = [self createBackButton];
        
    }
}

- (void)handleBackButton
{
    [self popViewControllerAnimated:YES];
}

@end
